package com.easy.base.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.AuthenticationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.easy.base.utils.VerifyCodeUtil;

import java.io.IOException;

@Controller
@RequestMapping("/")
@Api(tags = "根目录页面")
public class PageController {
    @Resource
    private HttpServletRequest request;

    @ApiOperation(value = "登录页")
    @GetMapping("Login")
    public String login() {
        return  "login";
    }

    @ApiOperation(value = "首页")
    @GetMapping("Index")
    public String index() {
        return "index";
    }

    @ApiOperation(value = "登录错误页")
    @GetMapping("/LoginError")
    public void loginError(HttpServletRequest request, HttpServletResponse response) {
        response.setContentType("text/html;charset=utf-8");
        AuthenticationException exception = (AuthenticationException)request.getSession().getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
        try {
            response.getWriter().write(exception.getMessage());
        }catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "无效页")
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @GetMapping("/Invalid")
    public String invalid() {
        return "invalid";
    }

    @ApiOperation(value = "请求验证码")
    @ResponseBody
    @GetMapping("GetVerifyCode")
    public ResponseEntity<String> getVerifyCode() {
        String verifyCode = VerifyCodeUtil.Generator();
        HttpSession session = request.getSession();
        session.setAttribute("validateCode", verifyCode);
        return ResponseEntity.ok().body(verifyCode);
    }

    @ApiOperation(value = "测试GET方式请求")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "message", value = "文本信息", dataTypeClass = String.class, paramType = "query")
    })
    @ResponseBody
    @GetMapping("RequestGetTest")
    public ResponseEntity<String> requestGetTest(String message) {
        if (StringUtils.isBlank(message))
            return ResponseEntity.ok().body("Request successful");
        else
            return ResponseEntity.ok().body("Request successful." + "Message is: " + message);
    }

    @ApiOperation(value = "测试POST方式请求")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "message", value = "文本信息", dataTypeClass = String.class, paramType = "query")
    })
    @ResponseBody
    @PostMapping("RequestPostTest")
    public ResponseEntity<String> requestPostTest(@RequestBody String message) {
        if (StringUtils.isBlank(message))
            return ResponseEntity.ok().body("Request successful");
        else
            return ResponseEntity.ok().body("Request successful." + "Message is: " + message);
    }
}
